package com.roc.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.NonUniqueResultException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.roc.bean.MenuBean;
import com.roc.dao.DaoException;
import com.roc.dao.IModelDao;
import com.roc.pojo.PowerModule;
import com.roc.pojo.ProfitModule;
import com.roc.pojo.ProfitPercentModule;
import com.roc.pojo.ProfitRuleModule;
import com.roc.util.Constance;
import com.roc.util.constant.ProfitModuleStatus;

@Repository
public class ModelDaoImpl extends BaseDaoImpl implements IModelDao{

	@Override
	public List<MenuBean> listAllMenus() throws DaoException {
		// TODO Auto-generated method stub
		List<MenuBean> menuList=new ArrayList<MenuBean>();
		try {
			String hql="select c.moduleCategoryCode,c.moduleCategoryName ,m.moduleCode,m.moduleName,c.displayIndex from ModuleCategory c,Module m where c.moduleCategoryCode=m.categoryCode "
				+" order by c.displayIndex asc,m.displayIndex asc ";
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createQuery(hql);
			
			Iterator iterator=query.list().iterator();
			while(iterator.hasNext()){
				MenuBean bean=new MenuBean();
				Object[] obj=(Object[]) iterator.next();
				bean.setCategoryCode((String)obj[0]);
				bean.setCategoryName((String)obj[1]);
				bean.setModuleCode((String)obj[2]);
				bean.setModuleName((String)obj[3]);
				bean.setCategoryIndex((Long)obj[4]);
				menuList.add(bean);
				
			}
		} catch (Exception e) {
			throw new DaoException("加载菜单失败",e);
		}
		
		return menuList;
	}

	@Override
	public List<PowerModule> queryModuleByOperatorId(Long operId,String modelCode) throws DaoException {
		// TODO Auto-generated method stub
		String hql="from PowerModule p where p.operatorsId="+operId+" and p.modelCode='"+modelCode+"'";
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery(hql);
		List<PowerModule> moduleList=query.list();
		
		return moduleList;
	}

	@Override
	public ProfitModule queryProfitModuleById(String modelId) throws DaoException {
		ProfitModule result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("from ProfitModule p where p.id = ? ");
	//		query.setLong("id", Long.valueOf(modelId));
			query.setString(0, modelId);
			result = (ProfitModule) query.uniqueResult();
		}catch(NonUniqueResultException nure){
			logger.debug("查询ProfitModule不唯一", nure);
			throw new DaoException("查询代理商分润模板不唯一");
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询代理商分润模板出错");
		}
		return result;
	}

	@Override
	public ProfitModule queryProfitModuleByModuleName (String moduleName) throws DaoException {
		ProfitModule result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("from ProfitModule p where p.moduleName = ? ");
	//		query.setLong("id", Long.valueOf(modelId));
			query.setString(0, moduleName);
			result = (ProfitModule) query.uniqueResult();
		}catch(NonUniqueResultException nure){
			logger.debug("查询ProfitModule不唯一", nure);
			throw new DaoException("查询代理商分润模板不唯一");
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询代理商分润模板出错");
		}
		return result;
	}

	@Override
	public List<ProfitModule> queryProfitModuleByAgentId (String agentId) throws DaoException {
		List<ProfitModule> profitRuleModuleList=new LinkedList<ProfitModule>();
		try {
			Session session = sessionFactory.getCurrentSession();
			StringBuffer sb=new StringBuffer("from ProfitModule p where p.agentId = ? ");
			
			Query query=session.createQuery(sb.toString());
			query.setString(0, agentId);

			profitRuleModuleList=query.list();
		}catch (Exception e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询代理商分润规则模板出错");
		}
		return profitRuleModuleList;
	}
	
	@Override
	public List<ProfitRuleModule> queryProfitRuleModule(String id) throws DaoException {
		List<ProfitRuleModule> profitRuleModuleList=new LinkedList<ProfitRuleModule>();
		try {
			Session session = sessionFactory.getCurrentSession();
			StringBuffer sb=new StringBuffer("from ProfitRuleModule p where p.profitModuleId = ? order by p.mccTypeCode");
			
			Query query=session.createQuery(sb.toString());
			query.setString(0, id);

			profitRuleModuleList=query.list();
		}catch (Exception e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询代理商分润规则模板出错");
		}
		return profitRuleModuleList;
	}
	@Override
	public List<ProfitPercentModule> queryProfitPerModule(String agentId) throws DaoException {
		List<ProfitPercentModule> profitPerModuleList=new LinkedList<ProfitPercentModule>();
		try {
			Session session = sessionFactory.getCurrentSession();
			StringBuffer sb=new StringBuffer("from ProfitPercentModule p where p.profitModuleId = ? order by p.percent ");
			
			Query query=session.createQuery(sb.toString());
			query.setString(0, agentId);

			profitPerModuleList=query.list();
		}catch (Exception e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询代理商分润比例模板出错");
		}
		return profitPerModuleList;
	}


	@Override
	public void deleteProfitModuleByModelId(String modelId) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if( null != modelId && !"".equals(String.valueOf(modelId))){
				String sql = "delete from t_profit_module where  id = " + modelId;
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("删除代理商分润模板出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除代理商分润模板出错");
		}
	}

	@Override
	public void deleteProfitRuleModuleByModelId(String modelId) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if(modelId!=null &&!"".equals(modelId)){
				String sql = "delete from t_profit_rule_module p where p.PROFIT_MODULE_ID='" + modelId + "'";
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("删除代理商分配规则模板出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除代理商分配规则模板出错");
		}
	}
	

	@Override
	public void deleteProfitPerModuleByModelId(String modelId) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if(modelId != null && !"".equals(modelId)){
				String sql = "delete from t_profit_percent_module p where p.PROFIT_MODULE_ID = '" + modelId + "'";
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("删除代理商分润比例模板出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除代理商分润比例模板出错");
		}
	}

	@Override
	public ProfitModule selDefProfitModuleByAgentId(long agentId) throws DaoException {
		ProfitModule result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("from ProfitModule p where p.status = "+ ProfitModuleStatus.YES.getCode() +" and p.agentId = '" + agentId + "' ");
	//		query.setLong("id", Long.valueOf(modelId));
	//		query.setString(0, status);
			result = (ProfitModule) query.uniqueResult();
		}catch(NonUniqueResultException nure){
			logger.debug("查询ProfitModule不唯一", nure);
			throw new DaoException("通过状态查询代理商分润模板不唯一");
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("通过状态查询代理商分润模板出错");
		}
		return result;
	}
	
	
	@Override
	public void updateProfitModuleStatusById(String modelId , String status) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if(modelId != null && !"".equals(modelId)){
				String sql = "update t_profit_module p set p.status = '" + status + "' where p.id = '" + modelId + "' ";
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("修改代理商分润模板状态出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("修改代理商分润模板状态出错");
		}
	}
}
